<template>
	<div>
		<!-- <el-tree :data="permiss" show-checkbox ref="tree" node-key="id" :default-expand-all="true" :default-checked-keys="defaultchecked" :props="{ label: 'name' }"></el-tree> -->
		<el-tree :data="tree"  show-checkbox :default-expand-all="true" :props="defaultProps" :default-checked-keys="defaultchecked" node-key="id" ref="tree" >
			<span class="custom-tree-node" slot-scope="{ node, data }" style="width: 100%;display: flex;align-items: center;justify-content: space-between;">
				<span>{{data.name}}</span>
			</span>
		</el-tree>
		<el-button type="primary" plain @click="getCheckedKeys()" style="margin-top: 30px;">保存</el-button>
	</div>
</template>

<script>
import axios from '../../utils/axiosutils.js';
export default {
	name: 'RolePermiss',
	props: ['editid'],
	data() {
		return {
			defaultProps: {
				label: 'name',
				children: 'children'
			},
			tree:[],
			permiss: [],
			defaultchecked: [],
			form: {
				id: '',
				permissionId: []
			}
		};
	},
	created() {    
		this.form.id = this.editid;
		this.getData();
		// this.getPermiss();
		this.getPermiss2();
	},
	methods: {
		getData() {
			axios.get(
				'/rolePermission/getByRoleId',
				response => {
					if (response.obj) {
						if(response.obj!=null){
							this.defaultchecked = response.obj.split(',').map(Number);
							console.log(this.defaultchecked)
						}
					}
				},
				{ id: this.form.id }
			);
		},
		getPermiss() {
			axios.get('/rolePermission/get-permiss', response => {
				this.permiss = response.obj;
			});
		},
		getPermiss2() {
			axios.get('/rolePermission/getPermiss', response => {
				this.tree = response.obj;
			});
		},
		// getCheckedKeys() {
		// 	this.form.permissionId = this.$refs.tree.getCheckedKeys();
		// 	axios.post(
		// 		'/rolePermission/add',
		// 		response => {
		// 			this.$emit('update:show', false);
		// 		},
		// 		{
		// 			id: this.form.id,
		// 			permissionId: this.form.permissionId
		// 		}
		// 	);
		// },
		getCheckedKeys () {
			this.form.permissionId=this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys());
			axios.post(
				'/rolePermission/add',
				response => {
					this.$emit('update:show', false);
				},
				{
					id: this.form.id,
					permissionId: this.form.permissionId
				}
			);
		}
		
	}
};
</script>
<style scoped></style>
